﻿using System;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;

namespace TestExtensions
{
    public abstract class BrowserBase
    {
        #region Variables

        private Uri _baseUri;
        internal BrowserWindow _browser;

        #endregion

        #region Constructors

        public BrowserBase(string url)
        {
            _baseUri = new Uri(url, UriKind.Absolute);
            _browser = InternetExplorerFactory.Open(url);
            Document = _browser.Find<HtmlDocument>();
        }

        #endregion

        #region Properties

        public HtmlDocument Document { get; private set; }

        #endregion

        #region Methods

        public void Close()
        {
            _browser.Close();
        }

        public void NavigateToUrl(string relativeUrl)
        {
            if (Document.AbsolutePath != relativeUrl)
            {
                _browser.NavigateToUrl(new Uri(_baseUri, new Uri(relativeUrl, UriKind.Relative)));
            }
        }

        public void Sync()
        {
            _browser.WaitForControlReady();
        }

        #endregion
    }
}